﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using ActivityTracker.Library;
using System.Xml.Linq;
using System.Linq;
using System.IO;

namespace ActivityTracker
{
    public class ActivityData
    {
        #region Global

        IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
        UserImage objUserImage = new UserImage();
        Category objCategory = new Category();

        #endregion

        public void ActivityOldDataClearing()
        {
            App.actName = null;
            App.ImageBytes = null;
            App.actId = null;
            App.AcitivityImgSrc = null;
            App.LocTrackingStatus = false;
            if (isoStore.FileExists("ActivityTracker/TempCategories.xml"))
                isoStore.DeleteFile("ActivityTracker/TempCategories.xml");
        }

      

        public List<LapProp> GenerateLaps(string actName, DateTime actStartDate, DateTime lapStartDate, string lapTime, int lapCount, List<LapProp> objLapProp)
        {
            try
            {
                if (lapTime != "00:00:00")
                    objLapProp.Add(new LapProp { ActId = App.runningActId, ActName = actName, ActStartDate = actStartDate, LapId = lapCount, LapNo = "lap " + lapCount, LapStartDate = lapStartDate, LapTime = lapTime });
            }
            catch (Exception ex)
            {
                Exception exNew = new Exception("Exception in GenerateLaps Method In ActivityData file\n\n" + ex.Message + " \n\n Stack Trace \n" + ex.StackTrace);
                exNew.Data.Add("actName_GenerateLaps", actName);
                exNew.Data.Add("actStartDate_GenerateLaps", actStartDate);
                exNew.Data.Add("lapStartDate_GenerateLaps", lapStartDate);
                exNew.Data.Add("lapTime_GenerateLaps", lapTime);
                exNew.Data.Add("lapCount_GenerateLaps", lapCount);
                throw exNew;
            }
            return objLapProp;
        }

        public void LoadSelectedActivity(string actId)
        {
            try
            {
                string imgName = "";
                if (isoStore.FileExists("ActivityTracker/MyActivities.xml"))
                {
                    IsolatedStorageFileStream isostream = new IsolatedStorageFileStream("ActivityTracker/MyActivities.xml", FileMode.Open, isoStore);
                    XDocument xdoc = XDocument.Load(isostream);
                    isostream.Close();
                    var ActList = from t in xdoc.Descendants("activity") where t.Attribute("id").Value == actId select t;
                    foreach (var Act in ActList)
                    {
                        App.actName = Act.Element("name").Value;
                        imgName = Act.Element("image").Value.ToString();

                        if (imgName != "")
                            App.AcitivityImgSrc = objUserImage.LoadActivityImages(Act.Attribute("id").Value.ToString(), Act.Element("image").Value.ToString()) as ImageSource;
                        else
                            App.AcitivityImgSrc = objUserImage.LoadActivityImages("0", "AddImage.png") as ImageSource;

                        App.LocTrackingStatus = Convert.ToBoolean(Act.Element("locationtracking").Value);

                        foreach (var cat in Act.Descendants("categories").Elements())
                            objCategory.saveCategoriesTemp(cat.Value.ToString(), cat.Attribute("catid").Value.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                Exception exnew = new Exception("Exception in LoadSelectedActivity Method In ActivityData file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace);
                ex.Data.Add("actId_LoadSelectedActivity", actId);
                throw exnew;
            }
        }
    }
}
